/*
 * @Author: 矫建武 1452239416@qq.com
 * @Date: 2025-02-17 16:32:37
 * @LastEditTime: 2025-02-18 09:28:54
 * @FilePath: \toolDb\db\uiComponents.js
 * @Description: 
 * 
 * @file: https://github.com/OBKoro1/koro1FileHeader/wiki/%E5%AE%89%E8%A3%85%E5%92%8C%E5%BF%AB%E9%80%9F%E4%B8%8A%E6%89%8B
 */

const dbConfig = require("./index")
const dayjs = require('dayjs')
const { Parameters } = require('../utils/utils')
const getUiComponentsList = (req, res) => {
  let sql = `select * from tool.uiComponents`
  let sqlArr = []
  dbConfig.sqlConnect(sql, sqlArr, (data, err) => {
    if (err) return res.sends('参数错误')
    //返回数据
    res.sends(data.map(ele => {
      return { ...ele, parameter: JSON.parse(ele.parameter) }
    }))
  }, res)
}

const addUiComponents = (req, res) => {
  const { keys, values, num } = new Parameters({ ...req.body, parameter: JSON.stringify(req.body.parameter) }).add({ userId: req.userId, createTime: dayjs().format('YYYY-MM-DD') })
  const sql = `insert into tool.uiComponents(${keys}) values(${num})`
  dbConfig.sqlConnect(sql, [...values], (data, err) => {
    if (err) return res.sends(err)
    res.sends('成功')
  }, res)
}
const deleteUiComponents = (req, res) => {
  const sql = `delete from tool.uiComponents where id=?`
  dbConfig.sqlConnect(sql, [req.body.id], (data, err) => {
    if (err) return res.sends(err)
    res.sends('成功')
  }, res)
}

module.exports = { getUiComponentsList, addUiComponents, deleteUiComponents }
